<template>
	<view v-for="(item,index) in list" :key="index" @tap="openinfo" :data-id="item.id" :data-type="item.type">
		<ulist :title="item.type" :name='item.name' :idNumber="item.idCard" :tel="item.tel" :type="item.title"></ulist>
	</view>
</template>

<script>
	import ulist from '~@/components/ulist.vue'
	import request from '@/utils/api.js'
	export default {
		components: {
			ulist
		},
		data() {
			return {
				list: []
			}
		},
		onLoad() {
			let that = this
			let sort = async function() {
				let sortList = function() {
					let arr = JSON.parse(JSON.stringify(that.list))
					arr.sort((a, b) => {
						if (!('startTime' in a) || !('startTime' in b)) {
							return 1;
						}
						if (a.startTime > b.startTime) {
							return -1;
						} else if (a.startTime === b.startTime) {
							return 0;
						} else {
							return 1;
						}
					})
					console.log('arr', arr);
					that.list = arr
				}
				await sortList();
			}

			let arr = []
			let userInfo = uni.getStorageSync('userInfo')
			uni.showLoading({
					title: "加载中...."
				}),
				request('/application/in/list', {}, 'GET').then(resp => {
					arr = resp.data;
					arr.forEach((item, index) => {
						item["tel"] = userInfo.phone;
						item["type"] = "入校申请";
						//0 申请成功  1 申请失败  2 待处理  3 已失效
						if (item.state == 0) {
							item["title"] = "成功";
						} else if (item.state == 1) {
							item["title"] = "失败";
						} else if (item.state == 2) {
							item["title"] = "待处理";
						} else if (item.state == 3) {
							item["title"] = "已失效"
						}
						that.list.push(item);
					});
					uni.setStorage({
						key: 'inList',
						data: arr
					})
					sort()
					arr = []; //清空arr后留给后面的请求用
				}),
				request('/application/out/list', {}, 'GET').then(resp => {
					// console.log(resp.data);
					arr = resp.data;
					arr.forEach((item, index) => {
						item["tel"] = userInfo.phone;
						item["type"] = "离校申请";
						if (item.state == 0) {
							item["title"] = "成功";
						} else if (item.state == 1) {
							item["title"] = "失败";
						} else if (item.state == 2) {
							item["title"] = "待处理";
						} else if (item.state == 3) {
							item["title"] = "已失效"
						}
						that.list.push(item);
					});
					uni.setStorage({
						key: 'outList',
						data: arr
					})
					sort()
				})
		},
		methods: {
			openinfo(e) {
				// console.log(e);
				var id = e.currentTarget.dataset.id;
				var type = e.currentTarget.dataset.type;
				uni.navigateTo({
					url: '/pages/index/out-in/detail?type=' + type + '&id=' + id
				});
			}
		}
	}
</script>
